<div id="dltype-info-<{$dt_id}>">
<h3 class="head-title"><{t}>添加配送方式<{/t}></h3>
<form action="index.php?app=b2c&ctl=admin_dlytype&act=saveDlType" id="dtypeForm" method="post" class="tableform">
<div class="division">
    <table>
        <tr>
         <th><{t}>排序<{/t}>:</th>
         <td>
             <input  type="text" vtype="unsignedint" name="ordernum" id="ordernum" value="<{$dt_info.ordernum}>" size=5 caution="<{t}>请输入排序项，并且为正整数<{/t}>" class="_x_ipt"/>
         </td>
     </tr>
     <tr>
         <th><{t}>是否启用<{/t}>:</th>
         <td><label><input type="radio" name="dt_status" <{if $dt_info.dt_status=="1"||$dt_info.dt_status==""}>checked<{/if}> value="1"><{t}>启用<{/t}></label>&nbsp;&nbsp;
            <label><input type="radio" name="dt_status" value="0" <{if $dt_info.dt_status=="0"}>checked<{/if}>><{t}>关闭<{/t}></label>
        </td>
     </tr>

    </table>

</div>
<div class="division">
<{if $dt_info.dt_id}>
    <input type="hidden" name="dt_id" id="aEditor-Garticleid-input" value="<{$dt_info.dt_id}>" />
<{else}>
    <input type="hidden" name="dt_id" id="aEditor-Garticleid-input" value="" />
<{/if}>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <th><{t}>配送方式名称<{/t}>:</th>
    <td><input type="text" name="dt_name" value="<{$dt_info.dt_name}>" vtype="required" class="_x_ipt" caution="<{t}>请填写配送方式名称<{/t}>" maxlength="20"></td>
  </tr>
 <tr>
    <th><{t}>选择默认物流公司<{/t}>:</th>
    <td>
      <select name="corp_id">
        <option value="0"><{t}>请选择物流公司<{/t}>:</option>
      <{foreach from=$clist item=corp}>
        <option value='<{$corp.corp_id}>' <{if $dt_info.corp_id == $corp.corp_id}>selected<{/if}>><{$corp.name}></option>
      <{/foreach}>
      </select>
    </td>
  </tr>
  <tr>
    <th><{t}>类型<{/t}>:</th>
    <td>
    <input type='checkbox' name='has_cod' <{if !($dt_info.has_cod == 'false' or $dt_info == "")}>checked='checked'<{/if}><label><{t}>是否支持货到付款<{/t}></label>
    </td>
  </tr>
  <tr>
    <th><{t}>重量设置<{/t}>:</th>
    <td>
        <{t}>首重重量<{/t}>:<select name="firstunit" ><{html_options options=$weightunit selected=$dt_info.firstunit}></select>&nbsp;&nbsp;
        <{t}>续重单位<{/t}>:<select name="continueunit"><{html_options options=$weightunit  selected=$dt_info.continueunit}></select>
    </td>
  </tr>
  <tr>
    <th><{t}>默认配送费用<{/t}>:</th>
    <td>
    <div id="def_dexp">
        <input type="hidden" name="price" value=0 />
        <div class='deliveryexpbox'>
          <div class='deliveryexp' style="<{if $dt_info.dt_useexp==1}>display:none<{/if}>">
                   <label><{t}>首重费用<{/t}> <input style="width:40px;" type='text' name='firstprice' value="<{$dt_info.firstprice}>" class="_x_ipt" vtype="required&&number"/></label>&nbsp;&nbsp;
                   <label><{t}>续重费用<{/t}> <input style="width:40px;" type='text' name='continueprice' value="<{$dt_info.continueprice}>" class="_x_ipt" vtype="required&&number"/></label>
                    <{if !$is_delivery_discount_close}>
                    &nbsp;&nbsp;<label><{t}>邮寄折扣 <{/t}><input style="width:30px;" type='text' name='dt_discount' value="<{$dt_info.dt_discount}>" class="_x_ipt" vtype="required&&number"/></label>
                    <{/if}>
                    &nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>使用公式<{/t}></span>
          </div>
          <div class='deliveryexp' style='<{if $dt_info.dt_useexp==0}>display:none<{/if}>'>
            <{t}>配送公式<{/t}>
            <input style="width:70%;" type='text' name='dt_expressions' value="<{if $dt_info.dt_useexp}><{$dt_info.dt_expressions}><{/if}>"  class="_x_ipt" vtype="required&&checkExp1&&checkExp2" />
            &nbsp;&nbsp;
            <span class='lnk chgexp' onclick='checkExp(this)'><{t}>验证公式<{/t}></span>&nbsp;&nbsp;
            <span class='lnk chgexp' onclick='chaexps(this)'><{t}>取消公式<{/t}></span>
            <input type="hidden" name="dt_useexp" value="<{$dt_info.dt_useexp}>">
           </div>
        </div>
    </div>
    </td>
  </tr>
  <tr>
    <th><{t}>物流保价<{/t}>:</th>
    <td><label><input type="checkbox" name="protect" value="1" <{if $dt_info.protect=='true'}>checked<{/if}>><{t}>支持物流保价<{/t}></label>
        <span id="protect_rate">
             <label><{t}>费率<{/t}>:<input type="text" name="protect_rate" value="<{$dt_info.protect_rate}>" style="width:25px;" class='_x_ipt' vtype='required&&number'>%</label>&nbsp;&nbsp;
             <label><{t}>最低保价费<{/t}>:<input type="text" name="minprice" value="<{$dt_info.minprice}>" style="width:25px;" class='_x_ipt' vtype='required&&number'></label>
        </span>
    </td>
  </tr>
  <tr>
    <th><{t}>默认配送费用适用地区<{/t}>:</th>
    <td>
    <div id='deliveryAreaToggle'>
      <label><input type="radio" name="setting" value='1' <{if $dt_info.setting=="1"||$dt_info.setting==""}>checked<{/if}>><{t}>所有地区适用<{/t}></label>&nbsp;&nbsp;
      <label><input type="radio" name="setting" value='0' <{if $dt_info.setting=="0"}>checked<{/if}>><{t}>指定部分地区城市设置运费<{/t}></label>
    </div>
<div id='deliveryAreabox' style="padding:10px;background:#efefef;margin:10px 0;display:none;">
        <div style="background:#e9e9e9;line-height:22px;"><label><input type="checkbox" name="def_area_fee" value="1" <{if $dt_info.def_area_fee=='true'}>checked<{/if}>><{t}>除指定地区外，其余地区的运费采用“默认配送费用”<{/t}></label>&nbsp;&nbsp;
        <span class="notice-inline"><{t}>注意：未勾选时，不在指定配送地区的顾客不能使用本配送方式下订单<{/t}></span>
        </div>
        <div  id="deliveryArea-<{$dt_info.dt_id}>" class='deliveryArea'>
            <input type='hidden' name='delidgroup'/>
           <{if $dt_info.area_fee_conf}>
             <{foreach from=$dt_info.area_fee_conf item=area key=key}>
             <div class="area-item clearfix division">
                    <div class="span-auto"><{t}>配送地区<{/t}>:</div>
                    <div class="deliverycity span-6">
                        <span class="city-name"><{$area.areaGroupName|default:'请选择配送地区'}></span>
                        <input type='hidden' name='area_fee_conf[<{$key}>][areaGroupName]' value="<{$area.areaGroupName}>"/>
                        <input type='hidden' name='area_fee_conf[<{$key}>][areaGroupId]' value="<{$area.areaGroupId}>"/>
                        <span class="lnk" onclick="regionSelect(this)"><{t}>地区选择<{/t}></span>
                    </div>
                    <div class="deliveryexpbox span-12" acckey="<{$key}>">
                        <div class='deliveryexp' style="<{if $area.dt_useexp==1}>display:none<{/if}>">
                        <label><{t}>首重费用<{/t}>:<input style="width:40px;" type='text' name='area_fee_conf[<{$key}>][firstprice]' value="<{$area.firstprice}>" <{if $area.dt_useexp=="0"}>class="_x_ipt"<{/if}> vtype="required&&number"></label>
                        &nbsp;&nbsp;
                        <label><{t}>续重费用<{/t}>:<input style="width:40px;" type='text' name='area_fee_conf[<{$key}>][continueprice]' value="<{$area.continueprice}>" <{if $area.dt_useexp=="0"}>class="_x_ipt"<{/if}> vtype="required&&number" ></label>
                        <{if !$is_delivery_discount_close}>
                        &nbsp;&nbsp;<label><{t}>邮寄折扣<{/t}><input type="text" name="area_fee_conf[<{$key}>][dt_discount]" value="<{$area.dt_discount|default:1}>" style="width:30px;" class='_x_ipt' vtype='required&&number'></label>
                        <{/if}>
                        &nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>使用公式<{/t}></span>
                        </div>
                        <div class='deliveryexp' style='<{if $area.dt_useexp==0}>display:none<{/if}>'>
                        <{t}>配送公式<{/t}>:<input style="width:60%" type='text' name='area_fee_conf[<{$key}>][expressions]' value="<{$area.dt_expressions}>" <{if $area.dt_useexp=="1"}>class="_x_ipt"<{/if}> vtype="required&&checkExp1&&checkExp2" />
                            &nbsp;&nbsp;
                            <span class='lnk' onclick='checkExp(this)'><{t}>验证公式<{/t}></span>&nbsp;&nbsp;
                            <span class='lnk chgexp' onclick='chaexps(this)'><{t}>取消公式<{/t}></span>
                            <input type='hidden' name='area_fee_conf[<{$key}>][dt_useexp]' value='<{$area.dt_useexp}>'/>
                        </div>
                    </div>
                    <div class="frt">
                        <span class="lnk" onclick="deleteDelivery($(this).getParent('.area-item'))"><{t}>删除<{/t}></span>
                    </div>
            </div>
             <{/foreach}>
            <{/if}>
            <br/>
            <span class="lnk add-dlyarea">[+]<{t}>为指定的地区设置运费<{/t}></span>
            <textarea style="display:none;" disabled=true>
                <div class="span-auto"><{t}>配送地区<{/t}>:</div>
                <div class="deliverycity span-6">
                        <span class="city-name">请选择配送地区</span>
                        <input type='hidden' name='area_fee_conf[{key}][areaGroupName]' value="<{$area.areaGroupName}>"/>
                        <input type='hidden' name='area_fee_conf[{key}][areaGroupId]' value="<{$area.areaGroupId}>"/>
                        <span class="lnk" onclick="regionSelect(this)"><{t}>地区选择<{/t}></span>
                </div>
                <div class="deliveryexpbox span-12" acckey="{key}">
                    <div class='deliveryexp' style="<{if $area.dt_useexp==1}>display:none<{/if}>">
                    <label><{t}>首重费用<{/t}>:<input style="width:40px;" type='text' name='area_fee_conf[{key}][firstprice]' value="{firstprice}" class="_x_ipt" vtype="required&&number"></label>
                    &nbsp;&nbsp;
                    <label><{t}>续重费用<{/t}>:<input style="width:40px;" type='text' name='area_fee_conf[{key}][continueprice]' value="{continueprice}" class="_x_ipt" vtype="required&&number" ></label>
                    <{if !$is_delivery_discount_close}>
                    &nbsp;&nbsp;<label><{t}>邮寄折扣<{/t}><input type="text" name="area_fee_conf[{key}][dt_discount]" value="{dt_discount}" style="width:40px;" class='_x_ipt' vtype='required&&number'></label>
                    <{/if}>
                    &nbsp;&nbsp;&nbsp;&nbsp;<span class='lnk chgexp' onclick='chaexps(this)'><{t}>使用公式<{/t}></span>
                    </div>
                    <div class='deliveryexp' style='<{if $area.dt_useexp==0}>display:none<{/if}>'>
                    <{t}>配送公式<{/t}>:<input style="width:60%" type='text' name='area_fee_conf[{key}][expressions]' value="" class="_x_ipt" vtype="required&&checkExp1&&checkExp2" />
                        &nbsp;&nbsp;
                        <span class='lnk' onclick='checkExp(this)'><{t}>验证公式<{/t}></span>&nbsp;&nbsp;
                        <span class='lnk chgexp' onclick='chaexps(this)'><{t}>取消公式<{/t}></span>
                        <input type='hidden' name='area_fee_conf[{key}][dt_useexp]' value='0'/>
                    </div>
                </div>
                <div class="frt">
                    <span class="lnk" onclick="deleteDelivery($(this).getParent('.area-item'))"><{t}>删除<{/t}></span>
                </div>
            </textarea>
        </div>
</div>

    </td>
  </tr>
  </table>
</div>
    <{$extends_html}>

<div class="division">
    <table width="100%" border="0" cellpadding="0" cellspacing="0">
     <tr>
        <th><{t}>配送方式介绍：<{/t}></th>
        <td><{input type="html" name="detail" value=$dt_info.detail}></td>
      </tr>
    </table>
</div>

</form>
</div>


<script>
  var panel = $('dltype-info-<{$dt_id}>');
  panel.getElement('input[name=protect]').addEvent('click',function(){
      ($('protect_rate')[this.checked?'show':'hide']()).getFormElements().set('disabled',!this.checked);
  }).fireEvent('click');
  var daf = $('deliveryAreabox').getElement('input[name=def_area_fee]');
      daf.addEvent('click',function(){
        var f = !daf.checked&&!daf.disabled;
        $('def_dexp').setOpacity(f?0.5:1).getFormElements().set('disabled',f);
      });

var adddluarea = $$('#deliveryArea-<{$dt_info.dt_id}> .add-dlyarea').addEvent('click',function(){

        var fillData = {
            key:++$$('#deliveryArea-<{$dt_info.dt_id}> .area-item').length,
            firstprice:$('def_dexp').getElement('input[name=firstprice]').value,
            continueprice:$('def_dexp').getElement('input[name=continueprice]').value,
            dt_discount:$('def_dexp').getElement('input[name=dt_discount]')?$('def_dexp').getElement('input[name=dt_discount]').value:''
        };

        var tpl = this.getNext('textarea').value;

        new Element('div',{html:tpl.substitute(fillData)}).addClass('area-item clearfix division').injectBefore(this);

    });

  $$('#deliveryAreaToggle input[type=radio]').addEvent('click',function(){
        if(!this.checked)return;
        if(this.value>0){
                $('deliveryAreabox').hide().getFormElements().set('disabled',this.checked);
        }else{
                $('deliveryAreabox').show().getFormElements().set('disabled',!this.checked);

                if($$('#deliveryArea-<{$dt_info.dt_id}> .area-item').length<1){

                    adddluarea.fireEvent('click');
                }

        }
        daf.fireEvent('click');
  }).fireEvent('click');



var deleteDelivery = function(el){
    if (!confirm('<{t}>删除后无法恢复，确定要删除吗？<{/t}>')){
        return;
      }
    var areaid_group=el.getElement('input[name^=area_fee_conf]');
    var delidgroupHidden=$E('#deliveryArea-<{$dt_info.dt_id}> input[name=delidgroup]');
    var _id;
    areaid_group.get('name').replace(/\[(\d+)?\]/,function($0,$1){
          _id=$1;
    });
    if(!_id)return;
    delidgroupHidden.value+=(delidgroupHidden.value!=''?",":'')+_id;
    el.destroy();
}

var getval = function(expval){
  if (eval(expval) > 0.000001){
    return 1;
  }else if (eval(expval) >-0.000001&&eval(expval)< 0.000001){
    return 1/2;
  }else{
    return 0;
  }
}
var  getceil = function(expval){
  if (eval(expval) > 0){
    return Math.ceil(eval(expval)-0.000001);
  }else{
    return 0;
  }
}

validatorMap.set('checkExp1',['<{t}>公式中含有非法字符<{/t}>',function(e,v){
           return !(new RegExp("/^[^\]\[\}\{\)\(0-9WwPp\+\-\/\*]+$/")).test(v);
   }]);
validatorMap.set('checkExp2',['<{t}>公式格式不正确<{/t}>',function(e,v){
           var price = 100;
           var weight = 100;
           var str ;
              str = v.replace(/(\[)/g, "getceil(");
              str = str.replace(/(\])/g, ")");
              str = str.replace(/(\{)/g, "getval(");
              str = str.replace(/(\})/g, ")");
              str = str.replace(/(W)/g, weight);
              str = str.replace(/(w)/g, weight);
              str = str.replace(/(P)/g, price);
              str = str.replace(/(p)/g, price);
          try {
            eval(str);
            return true;
          }catch(e){
            return false;
          }
   }]);


var chaexps = function(el){
    var  dexpsbox = $(el).getParent('.deliveryexpbox');
    var arr_depxs = dexpsbox.getElements('.deliveryexp');
    arr_depxs.each(function(obj){
        obj.setStyle('display','none'==obj.getStyle('display')?'':'none');
        if (obj.getElement('input[name^=useexp]'))
        {
            if (!obj.isDisplay())
            {
                obj.getElement('input[name^=useexp]').set('value',0);
            }
            else
            {
                obj.getElement('input[name^=useexp]').set('value',1);
            }
        }

        if (obj.getElement('input[name^=dt_useexp]'))
        {
            if (!obj.isDisplay())
            {
                obj.getElement('input[name^=dt_useexp]').set('value',0);
            }
            else
            {
                obj.getElement('input[name^=dt_useexp]').set('value',1);
            }
        }

        if (obj.getElement('input[name^=\'area_fee_conf[' + dexpsbox.get('acckey') + '][useexp]\']'))
        {
            if (!obj.isDisplay())
            {
                obj.getElement('input[name^=\'area_fee_conf[' + dexpsbox.get('acckey') + '][useexp]\']').set('value',0);
            }
            else
            {
                obj.getElement('input[name^=\'area_fee_conf[' + dexpsbox.get('acckey') + '][useexp]\']').set('value',1);
            }
        }

        if (obj.getElement('input[name^=\'area_fee_conf[' + dexpsbox.get('acckey') + '][dt_useexp]\']'))
        {
            if (!obj.isDisplay())
            {
                obj.getElement('input[name^=\'area_fee_conf[' + dexpsbox.get('acckey') + '][dt_useexp]\']').set('value',0);
            }
            else
            {
                obj.getElement('input[name^=\'area_fee_conf[' + dexpsbox.get('acckey') + '][dt_useexp]\']').set('value',1);
            }
        }

    });

}

var checkExp=function(btn){
       btn=$(btn);
       var ipt=btn.getPrevious('input');
       var expValue=ipt.getValue();
       new Dialog('index.php?app=b2c&ctl=admin_dlytype&act=checkExp&expvalue='+encodeURIComponent(expValue),
       { modal:true,
        title:'<{t}>验算配送公式<{/t}>',
        onShow:function(){

            this.dialog.store('targetIpt',ipt);
         }
       }
       );
   }


var regionSelect = function(el){
       var el=$(el).getParent('.deliverycity');
       var ipt = el.getElements('input[type=hidden]');

    Ex_Loader('modedialog',function(){
      new ModeDialog('index.php?app=ectools&ctl=regions&act=showRegionTreeList&p[0]='+(+new Date)+'&p[1]=multi',{
          width:270,height:window.getSize().y*.9,params:{iptText:ipt[0],iptHidden:ipt[1]}});
     });
}

$('dtypeForm').store('target',{
    onComplete:function(){
        if(opener.finderGroup['<{$env.get._finder.finder_id}>'])
        opener.finderGroup['<{$env.get._finder.finder_id}>'].refresh();
        window.close();
    }
});
subDtypeForm = function (event,sign){
   var target={};
   var _target=$(new Event(event).target);
   switch (sign){
        case 1:                    //保存不关闭
            $extend(target,{
                onComplete:function(e){
                    if(window.opener.finderGroup&&window.opener.finderGroup['<{$env.get.finder_id}>'])
                    window.opener.finderGroup['<{$env.get.finder_id}>'].refresh();

                    if(!JSON.decode(e).dt_id){
                        _target.removeProperty('disabled');
                        return;
                    }
                    var id = JSON.decode(e).dt_id;
                    if(id > 0){
                        $('aEditor-Garticleid-input').value = JSON.decode(e).dt_id
                    }
                    _target.disabled = false;
                }}
            );
        break;
        case 2:                   //保存关闭
            $extend(target,{
                onComplete:function(){
                    if(window.opener.finderGroup&&window.opener.finderGroup['<{$env.get.finder_id}>'])
                    window.opener.finderGroup['<{$env.get.finder_id}>'].refresh();
                    window.close();
                }}
            );
        break;
   }
    var _form=$('dtypeForm');
    if(!_form)return;
    var _formActionURL=_form.get('action');

    _form.store('target',target);
    _form.set('action',_formActionURL+'&but='+sign).fireEvent('submit',new Event(event));
}



</script>

<{capture name='footbar'}>
<table cellspacing="0" cellpadding="0" style="margin:0 auto; width:100%" class="table-action">
      <tbody><tr valign="middle">
        <td>
            <{assign var="___db" value="关  闭"}>
            <{assign var="___dc" value=$___b2c='确定退出?'|t:'b2c'}>
            <{button label=$___b2c="保存并关闭窗口"|t:'b2c' class="btn-primary" onclick="subDtypeForm(event,2)"}>
            <{button label=$___b2c="保存当前"|t:'b2c' class="btn-primary" onclick="subDtypeForm(event,1)"}>
            <{button label=$___b2c=$___db|t:'b2c'  class="btn-secondary" onclick="if(confirm('{$___dc}'))window.close()"}>
        </td>
        </tr>
        </tbody></table>
<{/capture}>
